package com.xql.framework.action;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by apple on 15/3/22.
 */
public abstract class ActionSupport extends HttpServlet{
    private boolean isRedirect = false;
    private String url;

    /**
     * 默认调用方法
     * @return
     */
    public  String execute(){
        return "success";
    }
    public  String add(){
        return "success";
    }
    public  String edit(){
        return "success";
    }
    public  String delete(){
        return "success";
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if(isRedirect()){
            resp.sendRedirect(url);
        }else {
            RequestDispatcher rd = req.getRequestDispatcher(url);
            rd.forward(req, resp);
        }
    }



    public void setRedirect(boolean isRedirect){
        this.isRedirect = isRedirect;
    }
    public boolean isRedirect(){
        return this.isRedirect;
    }
    public void setUrl(String url){
        this.url=url;
    }
}
